import java.util.Arrays;
import java.util.Map;

/**
 * @author LKQ
 * @date 2021/11/25 11:46
 * @description 用哈希表
 */
public class HashMap {
    public static void main(String[] args) {
        int[] nums = {-1,-2,-3,-4,-5};
        int target = -8;
        System.out.println(Arrays.toString(twoSum(nums, target)));
    }
    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new java.util.HashMap<>();
        int[] result = new int[2];
        for (int i = 0; i < nums.length; i++) {
            int temp = target - nums[i];
            if (map.containsKey(temp)) {
                result[1] = i;
                result[0] = map.get(temp);
            }
            map.put(nums[i], i);
        }
        return result;
    }
}
